import * as Cesium from 'cesium';

class AirLineClass {
  constructor(viewer) {
    this.viewer = viewer;
    this.applyAirLineEntArr = []
    this.applyAirLineGrid = []
    let dirArrX = ['x', 'xy', 'xz', 'xyz', '-x', '-xy', '-xz', '-xyz']
    let dirArrY = ['y', 'xy', 'yz', 'xyz', '-y', 'x-y', '-yz', 'x-yz']
    let dirArrZ = ['z', 'xz', 'yz', 'xyz', '-z', 'x-z', 'y-z', 'xy-z']
    this.dirArr = [...new Set([...dirArrX, ...dirArrY, ...dirArrZ])];

    this.origin = [117.1213838888, 40.1407018888, 20000]
    this.gSize = [0.0001, 0.0001, 15]
    this.flyRun = [0.00001, 1000]
    this.llaLineArr = [[117.0293221, 40.3169491, 0],
    [117.0293221, 40.3169491, 120], [117.0793221, 40.3169491, 120],
    [117.0793221, 40.3309491, 120], [117.0953221, 40.3309491, 120],
    [117.0953221, 40.3259491, 120], [117.1003221, 40.3309491, 120],
    [117.1251988, 40.3206287, 120], [117.1251988, 40.2956287, 120],
    [117.1151988, 40.2956287, 120], [117.0851988, 40.3156287, 120],
    [117.0851988, 40.2856287, 120], [117.0966855, 40.2747555, 120],
    [117.0966855, 40.2747555, 0]]
    this.GridTrackArr = []
    this.GridEntitiesList = [[], [], []]
  }
  addAirLine () {
    const linePosXYZArr = Cesium.Cartesian3.fromDegreesArrayHeights(this.llaLineArr.flat())
    this.applyAirLineEntArr.push(this.viewer.entities.add({ polyline: { positions: linePosXYZArr, width: 5, material: Cesium.Color.GREEN } }))
    this.llaLineArr.forEach((element, index) => {
      this.testPos(Cesium.Cartesian3.fromDegrees(element[0], element[1], element[2]), index)
    });
    this.viewer.zoomTo(this.applyAirLineEntArr)
  }
  drawAirLine (list, str) {
    list.forEach((element, index) => {
      if (index === 43) {
        console.log(index, element);
      }
      this.testPos(Cesium.Cartesian3.fromDegrees(element[0], element[1], element[2]), index)
    });
    this.testPos(Cesium.Cartesian3.fromDegrees(list[0][0], list[0][1], list[0][2]), str, 'WHEAT')
    const linePosXYZArr = Cesium.Cartesian3.fromDegreesArrayHeights(list.flat())
    this.applyAirLineEntArr.push(this.viewer.entities.add({ polyline: { positions: linePosXYZArr, width: 5, material: Cesium.Color.GREEN } }))
  }
  drawCellGrid (pos) {
    const cosLat = Math.cos(Cesium.Math.toRadians(pos[1]));
    const deltaLon = this.gSize[0];
    const deltaLat = this.gSize[1] * cosLat;
    return [
      // 底面(红)
      [
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2]],
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2]],
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2]]
      ],
      // 上面(蓝)
      [
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]],
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]]
      ],
      // 前面(白色)
      [
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2]],
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2]]
      ],
      // 后面(黄色)
      [
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2]],
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2]]
      ],
      // 左面(黑色)
      [
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]],
        [pos[0] - deltaLon, pos[1] - deltaLat, pos[2]],
        [pos[0] - deltaLon, pos[1] + deltaLat, pos[2]]
      ],
      // 右面(紫色)
      [
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2] + this.gSize[2]],
        [pos[0] + deltaLon, pos[1] + deltaLat, pos[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2]],
        [pos[0] + deltaLon, pos[1] - deltaLat, pos[2] + this.gSize[2]]
      ]
    ]
  }
  testPos (xyz, str, colorStr = 'GREEN') {
    this.viewer.entities.add({
      position: xyz,
      point: {
        pixelSize: 10, // 点的大小（像素）
        color: Cesium.Color[`${colorStr}`], // 点的颜色
        outlineColor: Cesium.Color.BLACK, // 点的轮廓颜色
        outlineWidth: 2, // 点的轮廓宽度
      },
      label: {
        text: `${str}`, // 标签文本
        font: '14pt sans-serif', // 标签字体
        style: Cesium.LabelStyle.FILL_AND_OUTLINE, // 标签样式
        fillColor: Cesium.Color[`${colorStr}`], // 填充颜色
        outlineColor: Cesium.Color.BLACK, // 轮廓颜色
        outlineWidth: 2, // 轮廓宽度
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // 垂直方向对齐方式
        pixelOffset: new Cesium.Cartesian2(0, -10) // 标签位置偏移
      }
    });
  }
  drawOneBox (posArr, posIndex) {
    const dob = this.viewer.entities.add({
      polygon: {
        hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(posArr.flat())),
        material: Cesium.Color.GREEN.withAlpha(0.5), // 设置平面的颜色和透明度
        height: posArr[0][2],// 设置高度
        extrudedHeight: posArr[0][2] + this.gSize[2], // 拉伸高度 = 底部高度 + 50米
        closeTop: true, // 是否封闭顶部
        closeBottom: true, // 是否封闭底部
        outline: true, // 显示线条
        outlineColor: Cesium.Color.BLACK // 线条颜色
      }
    });
    dob.name = `AirBox_${posIndex}`
    dob.className = 'AirOneBox'
    this.GridEntitiesList[1].push(dob)
  }
  drawOnePlane (pos4, posIndex) {
    const dop = this.viewer.entities.add({
      polygon: {
        hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights(pos4.flat()),
        material: Cesium.Color.GREEN.withAlpha(0.1),
        perPositionHeight: true,
        outline: true,
      }
    })
    dop.name = `AirOnePlane_${posIndex}`
    dop.className = 'AirOnePlane'
    this.GridEntitiesList[2].push(dop)
  }
  drawOneLongBox (pos4, heightArr, posIndex) {
    console.log('drawOneLongBox->', posIndex);
    let isHeight = 0
    isHeight = heightArr[0] + heightArr[1]
    const dolb = this.viewer.entities.add({
      polygon: {
        hierarchy: new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(pos4.flat())),
        material: Cesium.Color.GREEN.withAlpha(0.5), // 设置平面的颜色和透明度
        height: heightArr[0],// 设置高度
        extrudedHeight: isHeight, // 拉伸高度 = 底部高度 + 50米
        closeTop: true, // 是否封闭顶部
        closeBottom: true, // 是否封闭底部
        outline: true, // 显示线条
        outlineColor: Cesium.Color.BLACK // 线条颜色
      }
    });
    dolb.name = `AirBox_${posIndex}`
    dolb.className = 'AirOneLongBox'
    this.GridEntitiesList[0].push(dolb)
  }
  // 上面绘制盒子
  topRectangleBox (posArr, diffArr, posIndex) {
    const gridTrackArr_T = [posArr[0], posArr[1]]
    this.drawOneLongBox(this.drawCellGrid(posArr[0])[0], [0, diffArr[2] + this.gSize[2] / 2], posIndex)
    for (let index = 1; index <= diffArr[2] / this.gSize[2] + 1; index++) {
      let pos = [posArr[0][0], posArr[0][1], posArr[0][2] + index * this.gSize[2]]
      let pos4 = this.drawCellGrid(pos)[0]
      if (this.GridTrackArr.length === 0) {
        pos4 = this.drawCellGrid([pos[0], pos[1], pos[2] - this.gSize[2] / 2])[0]
      }
      this.drawOnePlane(pos4, posIndex)
    }
    return gridTrackArr_T
  }
  // 下面绘制盒子
  bottomRectangleBox (posArr, diffArr, posIndex) {
    const gridTrackArr_B = [posArr[0], posArr[1]]
    console.log('gridTrackArr_B', posArr[0], posArr[1]);
    this.drawOneLongBox(this.drawCellGrid(posArr[1])[0], [0, diffArr[2] + this.gSize[2] / 2], posIndex)
    for (let index = 1; index <= diffArr[2] / this.gSize[2] + 1; index++) {
      let pos = [posArr[0][0], posArr[0][1], posArr[0][2] + 0.5 * this.gSize[2] - index * this.gSize[2]]
      let pos4 = this.drawCellGrid(pos)[1]
      this.drawOnePlane(pos4, posIndex)
    }
    return gridTrackArr_B
  }
  // 左面绘制盒子
  leftRectangleBox (posArr, diffArr, posIndex) {
    const gridTrackArr_L = [posArr[0], posArr[1]]
    const pos1_4 = this.drawCellGrid([posArr[0][0], posArr[0][1], posArr[0][2] - this.gSize[2] / 2])[4]
    this.testPos(Cesium.Cartesian3.fromDegrees(pos1_4[0][0], pos1_4[0][1], pos1_4[0][2]), 1)
    this.testPos(Cesium.Cartesian3.fromDegrees(pos1_4[1][0], pos1_4[1][1], pos1_4[1][2]), 2)
    const pos2_4 = this.drawCellGrid([posArr[1][0], posArr[1][1], posArr[1][2] - this.gSize[2] / 2])[4]

    const newPlanPos4 = [pos2_4[3], pos1_4[3], pos1_4[2], pos2_4[2]]
    this.drawOneLongBox(newPlanPos4, [newPlanPos4[0][2], this.gSize[2]], posIndex)
    const intervalNum = Math.ceil(diffArr[0] / this.gSize[0]) / 2
    for (let index = 1; index < intervalNum; index++) {
      let nppos4 = []
      pos1_4.forEach(element => { nppos4.push([element[0] - 2 * index * this.gSize[0], element[1], element[2]]) });
      this.drawOnePlane(nppos4, posIndex)
    }
    return gridTrackArr_L
  }
  // 右面绘制盒子
  rightRectangleBox (posArr, diffArr, posIndex) {
    const gridTrackArr_R = [posArr[0], posArr[1]]
    const pos1_4 = this.drawCellGrid([posArr[0][0], posArr[0][1], posArr[0][2] - this.gSize[2] / 2])[5]
    const pos2_4 = this.drawCellGrid([posArr[1][0], posArr[1][1], posArr[1][2] - this.gSize[2] / 2])[5]
    const newPlanPos4 = [pos1_4[1], pos2_4[1], pos2_4[2], pos1_4[2]]
    this.drawOneLongBox(newPlanPos4, [newPlanPos4[0][2], this.gSize[2]], posIndex)
    const intervalNum = Math.ceil(diffArr[0] / this.gSize[0]) / 2
    for (let index = 1; index < intervalNum; index++) {
      let nppos4 = []
      pos1_4.forEach(element => { nppos4.push([element[0] + 2 * index * this.gSize[0], element[1], element[2]]) });
      this.drawOnePlane(nppos4, posIndex)
    }
    return gridTrackArr_R
  }
  // 前面绘制盒子
  frontRectangleBox (posArr, diffArr, posIndex) {
    const gridTrackArr_F = [posArr[0], posArr[1]]
    const pos1_4 = this.drawCellGrid([posArr[0][0], posArr[0][1], posArr[0][2] - this.gSize[2] / 2])[2]
    const pos2_4 = this.drawCellGrid([posArr[1][0], posArr[1][1], posArr[1][2] - this.gSize[2] / 2])[2]
    const newPlanPos4 = [pos2_4[3], pos2_4[2], pos1_4[2], pos1_4[3]]
    this.drawOneLongBox(newPlanPos4, [newPlanPos4[0][2], this.gSize[2]], posIndex)
    const intervalNum = Math.ceil((diffArr[1] / this.gSize[1] / 1.5))
    for (let index = 1; index < intervalNum; index++) {
      let nppos4 = []
      pos1_4.forEach(element => { nppos4.push([element[0], element[1] + 1.5 * index * this.gSize[0], element[2]]) });
      this.drawOnePlane(nppos4, posIndex)
    }
    return gridTrackArr_F
  }
  // 后面绘制盒子
  backRectangleBox (posArr, diffArr, posIndex) {
    const gridTrackArr_B = [posArr[0], posArr[1]]
    const pos1_4 = this.drawCellGrid([posArr[0][0], posArr[0][1], posArr[0][2] - this.gSize[2] / 2])[3]
    const pos2_4 = this.drawCellGrid([posArr[1][0], posArr[1][1], posArr[1][2] - this.gSize[2] / 2])[3]
    const newPlanPos4 = [pos2_4[3], pos2_4[2], pos1_4[2], pos1_4[3]]
    this.drawOneLongBox(newPlanPos4, [newPlanPos4[0][2], this.gSize[2]], posIndex)
    const intervalNum = Math.ceil((diffArr[1] / this.gSize[1] / 1.5))
    for (let index = 1; index < intervalNum; index++) {
      let nppos4 = []
      pos1_4.forEach(element => { nppos4.push([element[0], element[1] - 1.5 * index * this.gSize[0], element[2]]) });
      this.drawOnePlane(nppos4, posIndex)
    }
    return gridTrackArr_B
  }
  // 左前方绘制盒子
  leftForeRectangleBox (posArr, diffArr, posIndex) {
    const gridTrackArr_LF = []
    const initH = posArr[0][2] - this.gSize[2] / 2
    // 已知的两个点（经纬度坐标）
    const point1 = posArr[1]; // 右上角
    const point2 = posArr[0]; // 左下角
    // 网格大小
    const gridSize = [this.gSize[0] * 2, this.gSize[0] * 1.5]; // 修改后的经度网格大小

    function BresenhamLine (x0, y0, x1, y1) {
      const points = [];
      let dx = Math.abs(x1 - x0);
      let dy = Math.abs(y1 - y0);
      let sx = (x0 < x1) ? 1 : -1;
      let sy = (y0 < y1) ? 1 : -1;
      let err = dx - dy;

      while (true) {
        points.push([y0, x0]); // 注意这里要以 (row, col) 的顺序返回
        if (x0 === x1 && y0 === y1) break;
        let e2 = err * 2;
        if (e2 > -dy) {
          err -= dy;
          x0 += sx;
        }
        if (e2 < dx) {
          err += dx;
          y0 += sy;
        }
      }
      return points;
    }

    // 计算行列索引的函数
    function calculateIndex (point, minLat, minLon, gridSize) {
      const row = Math.floor((point[1] - minLat) / gridSize[1]);
      const col = Math.floor((point[0] - minLon) / gridSize[0]);
      return [row, col];
    }

    // 将行列索引转换为经纬度的函数
    function indexToLatLon (row, col, minLat, minLon, gridSize) {
      const lat = minLat + (row + 0.5) * gridSize[1]; // 中心纬度
      const lon = minLon + (col + 0.5) * gridSize[0]; // 中心经度
      return [lon, lat];
    }

    // 定义最小的经纬度，即左下角
    const minLat = Math.min(point1[1], point2[1]);
    const minLon = Math.min(point1[0], point2[0]);

    // 计算两个点的索引
    const index1 = calculateIndex(point1, minLat, minLon, gridSize);
    const index2 = calculateIndex(point2, minLat, minLon, gridSize);
    const pathIndexes = BresenhamLine(index1[1], index1[0], index2[1], index2[0]);
    let newPathIndexes = []
    // 二次补点
    pathIndexes.forEach((element, index) => {
      newPathIndexes.push(element)
      if (index < pathIndexes.length - 2) {
        const dx = pathIndexes[index + 1][0] - pathIndexes[index][0]
        const dy = pathIndexes[index + 1][1] - pathIndexes[index][1]
        if (dx !== 0 && dy !== 0) {
          if (index < pathIndexes.length / 2 - 2) {
            if (diffArr[0] > diffArr[1]) {
              newPathIndexes.push([element[0], element[1] - 1])
            } else {
              newPathIndexes.push([element[0] - 1, element[1]])
            }
          } else {
            newPathIndexes.push([element[0] - 1, element[1]])
          }
        }
      }
    });
    newPathIndexes.forEach((element, index) => {
      const lonlatArr = indexToLatLon(element[0], element[1], minLat, minLon, gridSize)
      gridTrackArr_LF.push(lonlatArr.push(initH))
      this.drawOneBox(this.drawCellGrid([lonlatArr[0] + this.gSize[0], lonlatArr[1] - this.gSize[1] * 0.75, initH])[0], `${posIndex}_${index}`)
    });
    return gridTrackArr_LF
  }
  // 右前方绘制盒子
  rightForeRectangleBox (posArr, diffArr, posIndex) {
    const gridTrackArr_RF = []
    const initH = posArr[0][2] - this.gSize[2] / 2
    // 已知的两个点（经纬度坐标）
    const point1 = posArr[1]; // 右上角
    const point2 = posArr[0]; // 左下角
    // 网格大小
    const gridSize = [this.gSize[0] * 2, this.gSize[0] * 1.5]; // 修改后的经度网格大小

    function BresenhamLine (x0, y0, x1, y1) {
      const points = [];
      let dx = Math.abs(x1 - x0);
      let dy = Math.abs(y1 - y0);
      let sx = (x0 < x1) ? 1 : -1;
      let sy = (y0 < y1) ? 1 : -1;
      let err = dx - dy;

      while (true) {
        points.push([y0, x0]); // 注意这里要以 (row, col) 的顺序返回
        if (x0 === x1 && y0 === y1) break;
        let e2 = err * 2;
        if (e2 > -dy) {
          err -= dy;
          x0 += sx;
        }
        if (e2 < dx) {
          err += dx;
          y0 += sy;
        }
      }
      return points;
    }

    // 计算行列索引的函数
    function calculateIndex (point, minLat, minLon, gridSize) {
      const row = Math.floor((point[1] - minLat) / gridSize[1]);
      const col = Math.floor((point[0] - minLon) / gridSize[0]);
      return [row, col];
    }

    // 将行列索引转换为经纬度的函数
    function indexToLatLon (row, col, minLat, minLon, gridSize) {
      const lat = minLat + (row + 0.5) * gridSize[1]; // 中心纬度
      const lon = minLon + (col + 0.5) * gridSize[0]; // 中心经度
      return [lon, lat];
    }

    // 定义最小的经纬度，即左下角
    const minLat = Math.min(point1[1], point2[1]);
    const minLon = Math.min(point1[0], point2[0]);

    // 计算两个点的索引
    const index1 = calculateIndex(point1, minLat, minLon, gridSize);
    const index2 = calculateIndex(point2, minLat, minLon, gridSize);
    const pathIndexes = BresenhamLine(index1[1], index1[0], index2[1], index2[0]);
    let newPathIndexes = []
    // 二次补点
    pathIndexes.forEach((element, index) => {
      newPathIndexes.push(element)
      if (index < pathIndexes.length - 2) {
        const dx = pathIndexes[index + 1][0] - pathIndexes[index][0]
        const dy = pathIndexes[index + 1][1] - pathIndexes[index][1]
        if (dx !== 0 && dy !== 0) {
          if (index < pathIndexes.length / 2 - 2) {
            if (diffArr[0] > diffArr[1]) {
              newPathIndexes.push([element[0], element[1] - 1])
            } else {
              newPathIndexes.push([element[0] - 1, element[1]])
            }
          } else {
            newPathIndexes.push([element[0] - 1, element[1]])
          }
        }
      }
    });
    newPathIndexes.forEach((element, index) => {
      const lonlatArr = indexToLatLon(element[0], element[1], minLat, minLon, gridSize)
      gridTrackArr_RF.push(lonlatArr.push(initH))
      this.drawOneBox(this.drawCellGrid([lonlatArr[0] + this.gSize[0], lonlatArr[1] + this.gSize[1] * 0.75, initH])[0], `${posIndex}_${index}`)
    });
    return gridTrackArr_RF
  }
  // 左后方绘制盒子
  leftBackRectangleBox (posArr, diffArr, posIndex) {
    const gridTrackArr_LB = []
    const initH = posArr[0][2] - this.gSize[2] / 2
    // 已知的两个点（经纬度坐标）
    const point1 = posArr[0]; // 右上角
    const point2 = posArr[1]; // 左下角
    // 网格大小
    const gridSize = [this.gSize[0] * 2, this.gSize[0] * 1.5]; // 修改后的经度网格大小
    // 计算行列索引的函数
    function calculateIndex (point, minLat, minLon, gridSize) {
      const row = Math.floor((point[1] - minLat) / gridSize[1]);
      const col = Math.floor((point[0] - minLon) / gridSize[0]);
      return [row, col];
    }
    // 将行列索引转换为经纬度的函数
    function indexToLatLon (row, col, minLat, minLon, gridSize) {
      const lat = minLat + (row + 0.5) * gridSize[1]; // 中心纬度
      const lon = minLon + (col + 0.5) * gridSize[0]; // 中心经度
      return [lon, lat];
    }
    // 定义最小的经纬度，即左下角
    const minLat = Math.min(point1[1], point2[1]);
    const minLon = Math.min(point1[0], point2[0]);

    // 计算两个点的索引
    const index1 = calculateIndex(point1, minLat, minLon, gridSize);
    const index2 = calculateIndex(point2, minLat, minLon, gridSize);
    const pathIndexes = this.BresenhamLine(index1[1], index1[0], index2[1], index2[0]);
    let newPathIndexes = []
    // 二次补点
    pathIndexes.forEach((element, index) => {
      newPathIndexes.push(element)
      if (index < pathIndexes.length - 2) {
        const dx = pathIndexes[index + 1][0] - pathIndexes[index][0]
        const dy = pathIndexes[index + 1][1] - pathIndexes[index][1]
        if (dx === -1 && dy === -1) {
          if (index < pathIndexes.length / 2 - 2) {
            if (diffArr[0] > diffArr[1]) {
              newPathIndexes.push([element[0], element[1] - 1])
            } else {
              newPathIndexes.push([element[0] - 1, element[1]])
            }
          } else {
            newPathIndexes.push([element[0] - 1, element[1]])
          }
        }
      }
    });

    newPathIndexes.forEach((element, index) => {
      const lonlatArr = indexToLatLon(element[0], element[1], minLat, minLon, gridSize)
      gridTrackArr_LB.push(lonlatArr.push(initH))
      this.drawOneBox(this.drawCellGrid([lonlatArr[0] - this.gSize[0], lonlatArr[1] - this.gSize[1] * 0.75, initH])[0], `${posIndex}_${index}`)
    });
    return gridTrackArr_LB
  }
  // 右后方绘制盒子
  rightBackRectangleBox (posArr, diffArr, posIndex) {
    const gridTrackArr_RB = []
    const initH = posArr[0][2] - this.gSize[2] / 2
    // 已知的两个点（经纬度坐标）
    const point1 = posArr[0]; // 右上角
    const point2 = posArr[1]; // 左下角
    // 网格大小
    const gridSize = [this.gSize[0] * 2, this.gSize[0] * 1.5]; // 修改后的经度网格大小
    function BresenhamLine (x0, y0, x1, y1) {
      const points = [];
      let dx = Math.abs(x1 - x0);
      let dy = Math.abs(y1 - y0);
      let sx = (x0 < x1) ? 1 : -1;
      let sy = (y0 < y1) ? 1 : -1;
      let err = dx - dy;
      while (true) {
        points.push([y0, x0]); // 注意这里要以 (row, col) 的顺序返回
        if (x0 === x1 && y0 === y1) break;
        let e2 = err * 2;
        if (e2 > -dy) {
          err -= dy;
          x0 += sx;
        }
        if (e2 < dx) {
          err += dx;
          y0 += sy;
        }
      }
      return points;
    }
    // 计算行列索引的函数
    function calculateIndex (point, minLat, minLon, gridSize) {
      const row = Math.floor((point[1] - minLat) / gridSize[1]);
      const col = Math.floor((point[0] - minLon) / gridSize[0]);
      return [row, col];
    }
    // 将行列索引转换为经纬度的函数
    function indexToLatLon (row, col, minLat, minLon, gridSize) {
      const lat = minLat + (row + 0.5) * gridSize[1]; // 中心纬度
      const lon = minLon + (col + 0.5) * gridSize[0]; // 中心经度
      return [lon, lat];
    }
    // 定义最小的经纬度，即左下角
    const minLat = Math.min(point1[1], point2[1]);
    const minLon = Math.min(point1[0], point2[0]);
    // 计算两个点的索引
    const index1 = calculateIndex(point1, minLat, minLon, gridSize);
    const index2 = calculateIndex(point2, minLat, minLon, gridSize);
    const pathIndexes = BresenhamLine(index1[1], index1[0], index2[1], index2[0]);
    let newPathIndexes = []
    // 二次补点
    pathIndexes.forEach((element, index) => {
      newPathIndexes.push(element)
      if (index < pathIndexes.length - 2) {
        const dx = pathIndexes[index + 1][0] - pathIndexes[index][0]
        const dy = pathIndexes[index + 1][1] - pathIndexes[index][1]
        if (dx !== 0 && dy !== 0) {
          if (index < pathIndexes.length / 2 - 2) {
            if (diffArr[0] > diffArr[1]) {
              newPathIndexes.push([element[0] - 1, element[1]])
            } else {
              newPathIndexes.push([element[0] - 1, element[1]])
            }
          } else {
            newPathIndexes.push([element[0] - 1, element[1]])
          }
        }
      }
    });
    newPathIndexes.forEach((element, index) => {
      const lonlatArr = indexToLatLon(element[0], element[1], minLat, minLon, gridSize)
      gridTrackArr_RB.push(lonlatArr.push(initH))
      this.drawOneBox(this.drawCellGrid([lonlatArr[0] + this.gSize[0], lonlatArr[1] - this.gSize[1] * 0.75, initH])[0], `${posIndex}_${index}`)
    });
    return gridTrackArr_RB
  }
  // 使用 Bresenham 算法计算两点之间的网格索引
  BresenhamLine (x0, y0, x1, y1) {
    const points = [];
    let dx = Math.abs(x1 - x0);
    let dy = Math.abs(y1 - y0);
    let sx = (x0 < x1) ? 1 : -1;
    let sy = (y0 < y1) ? 1 : -1;
    let err = dx - dy;

    while (true) {
      points.push([y0, x0]); // 注意这里要以 (row, col) 的顺序返回
      if (x0 === x1 && y0 === y1) break;
      let e2 = err * 2;
      if (e2 > -dy) {
        err -= dy;
        x0 += sx;
      }
      if (e2 < dx) {
        err += dx;
        y0 += sy;
      }
    }
    return points;
  }
  // 更新盒子
  upLonLatAltGrid (pos, index) {
    console.log('upLonLatAltGrid->', pos, index);
  }
  // 初始化点位信息
  initLonLatAltGrid (lineArr) {
    this.llaLineArr = lineArr
    if (this.llaLineArr.length > 1) {
      for (let index = 0; index < this.llaLineArr.length - 1; index++) {
        this.llaLineArr[index]
        const spos = this.llaLineArr[index]
        const epos = this.llaLineArr[index + 1]
        const dx = epos[0] - spos[0]
        const dy = epos[1] - spos[1]
        const dh = epos[2] - spos[2]
        const dkey = `${dx > 0 ? '1' : `${dx < 0 ? '-1' : '0'}`}_${dy > 0 ? '1' : `${dy < 0 ? '-1' : '0'}`}_${dh > 0 ? '1' : `${dh < 0 ? '-1' : '0'}`}`
        const diffArr = [Math.abs(dx), Math.abs(dy), Math.abs(dh)]
        // 上
        if (dkey === '0_0_1') {
          this.GridTrackArr.push(this.topRectangleBox([spos, epos], diffArr, index))
        }
        // 上右
        if (dkey === '1_0_1') {
          console.log('上右', dkey);
        }
        // 上前右
        if (dkey === '1_1_1') {
          console.log('上前右', dkey);
        }
        // 下
        if (dkey === '0_0_-1') {
          this.GridTrackArr.push(this.bottomRectangleBox([spos, epos], diffArr, index))
        }
        // 左
        if (dkey === '-1_0_0') {
          this.GridTrackArr.push(this.leftRectangleBox([spos, epos], diffArr, index))
        }
        // 右
        if (dkey === '1_0_0') {
          this.GridTrackArr.push(this.rightRectangleBox([spos, epos], diffArr, index))
        }
        // 前
        if (dkey === '0_1_0') {
          this.GridTrackArr.push(this.frontRectangleBox([spos, epos], diffArr, index))
        }
        // 后
        if (dkey === '0_-1_0') {
          this.GridTrackArr.push(this.backRectangleBox([spos, epos], diffArr, index))
        }
        // 左前
        if (dkey === '-1_1_0') {
          this.GridTrackArr.push(this.leftForeRectangleBox([spos, epos], diffArr, index))
        }
        // 右前
        if (dkey === '1_1_0') {
          this.GridTrackArr.push(this.rightForeRectangleBox([spos, epos], diffArr, index))
        }
        // 左后
        if (dkey === '-1_-1_0') {
          this.GridTrackArr.push(this.leftBackRectangleBox([spos, epos], diffArr, index))
        }
        // 右后
        if (dkey === '1_-1_0') {
          this.GridTrackArr.push(this.rightBackRectangleBox([spos, epos], diffArr, index))
        }
      }
    }
    console.log('this.GridEntitiesList->', this.GridEntitiesList);
  }
  // 判断半球(用于解决世界坐标计算问题)
  getHemisphere (pos) {
    return pos[0] > 0 ? `E${pos[1] > 0 ? 'N' : 'S'}` : `W${pos[1] > 0 ? 'N' : 'S'}`
  }
}
export default AirLineClass;    